package com.example.android427.Fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.fragment.app.ListFragment;

import com.example.android427.DengluActivity;
import com.example.android427.R;
import com.example.android427.ShezhiActivity;
import com.example.android427.daikaifaActivity;
import com.example.android427.gerenziliaoActivity;



public class gerenFragment extends ListFragment{

    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
       ImageButton Shezhi = (ImageButton) getActivity().findViewById(R.id.shezhi);
        Shezhi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ShezhiActivity.class);
                startActivity(intent);
            }
        });

        final ImageButton touxaing_b2 = (ImageButton) getActivity().findViewById(R.id.touxiang_b2);
        touxaing_b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), gerenziliaoActivity.class);
                startActivity(intent);
            }
        });


       ImageButton genren_bt =  getActivity().findViewById(R.id.geren_jkh);
        genren_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), gerenziliaoActivity.class);
                startActivity(intent);
            }
        });

        ListView mListView = getActivity(). findViewById(android.R.id.list);
        // 添加ListItem，设置事件响应
        mListView.setAdapter(new DemoListAdapter());
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View v, int index,
                                    long arg3) {
                onListItemClick(index);
            }
        });

        ImageButton xianjin = getActivity().findViewById(R.id.xianjin_b1);
        xianjin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("请用读者卡登录");     //设置对话框标题
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getActivity(), "dialog" , Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setPositiveButton("去登录", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(getActivity(), DengluActivity.class);
                        startActivity(intent);
                    }
                });
                builder.setCancelable(true);   //设置按钮是否可以按返回键取消,false则不可以取
                AlertDialog dialog = builder.create();  //创建对话框
                dialog.setCanceledOnTouchOutside(true);      //设置弹出框失去焦点是否隐藏,即点击屏蔽其它地方是否隐
                dialog.show();
            }
        });

        ImageButton wodejieyue = getActivity().findViewById(R.id.wodejieyue_b1);
        wodejieyue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("请用读者卡登录");     //设置对话框标题
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getActivity(), "dialog" , Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setPositiveButton("去登录", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(getActivity(), DengluActivity.class);
                        startActivity(intent);
                    }
                });
                builder.setCancelable(true);   //设置按钮是否可以按返回键取消,false则不可以取
                AlertDialog dialog = builder.create();  //创建对话框
                dialog.setCanceledOnTouchOutside(true);      //设置弹出框失去焦点是否隐藏,即点击屏蔽其它地方是否隐
                dialog.show();
            }
        });

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.geren,container,false);
        return view;
    }

    void onListItemClick(int index) {
        Intent intent = null;
        intent = new Intent(getContext(), demos[index].demoClass);
        this.startActivity(intent);
    }

    private static final DemoInfo[] demos = {
            new DemoInfo("我的收藏",R.drawable.wdsc,R.drawable.right_jkh, daikaifaActivity.class),
            new DemoInfo("我的点赞",R.drawable.wddz,R.drawable.right_jkh, daikaifaActivity.class),
            new DemoInfo("我的足迹",R.drawable.wdzj, R.drawable.right_jkh,daikaifaActivity.class),
            new DemoInfo("我的收藏",R.drawable.wdsc,R.drawable.right_jkh, daikaifaActivity.class),
            new DemoInfo("我的点赞",R.drawable.wddz,R.drawable.right_jkh, daikaifaActivity.class),
            new DemoInfo("我的足迹",R.drawable.wdzj, R.drawable.right_jkh,daikaifaActivity.class),
            new DemoInfo("云同步",R.drawable.ytb, R.drawable.right_jkh,daikaifaActivity.class),
            new DemoInfo("其他应用",R.drawable.qtyy, R.drawable.right_jkh,daikaifaActivity.class),
            new DemoInfo("读者指南",R.drawable.dzzn, R.drawable.right_jkh, daikaifaActivity.class),
            new DemoInfo("常见问题",R.drawable.cjwt, R.drawable.right_jkh,daikaifaActivity.class),

    };

    private class DemoListAdapter extends BaseAdapter {
        public DemoListAdapter() {
            super();
        }
        @Override
        public View getView(int index, View convertView, ViewGroup parent) {
            convertView = View.inflate(getContext(),
                    R.layout.geren_item, null);
            ImageView img1 = convertView.findViewById(R.id.geren_img1);
            TextView t1 = convertView.findViewById(R.id.geren_t1);
            ImageView img2 = convertView.findViewById(R.id.geren_img2);
            img1.setImageResource(demos[index].imageId);
            t1.setText(demos[index].name);
            img2.setImageResource(demos[index].imageId1);
            return convertView;
        }

        @Override
        public int getCount() {
            return demos.length;
        }

        @Override
        public Object getItem(int index) {
            return demos[index];
        }

        @Override
        public long getItemId(int id) {
            return id;
        }
    }

    private static class DemoInfo {
        private String name;
        private int imageId;
        private int imageId1;
        private final Class<? extends android.app.Activity> demoClass;

        public DemoInfo(String name, int imageId,int imageId1,
                        Class<? extends android.app.Activity> demoClass) {
            this.name = name;
            this.imageId = imageId;
            this.imageId1 = imageId1;
            this.demoClass = demoClass;
        }
    }

}